package com.yyw.gulimall.product.controller;

import com.yyw.common.utils.PageUtils;
import com.yyw.common.utils.R;
import com.yyw.gulimall.product.entity.AttrEntity;
import com.yyw.gulimall.product.entity.AttrGroupEntity;
import com.yyw.gulimall.product.service.AttrAttrgroupRelationService;
import com.yyw.gulimall.product.service.AttrGroupService;
import com.yyw.gulimall.product.service.AttrService;
import com.yyw.gulimall.product.service.CategoryService;
import com.yyw.gulimall.product.vo.AttrAttrGroupVo;
import com.yyw.gulimall.product.vo.AttrGroupRelationVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 属性分组
 *
 * @author yiyw
 * @date 2020-10-24 11:59:19
 */
@RestController
@RequestMapping("product/attrgroup")
//@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class AttrGroupController {

    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AttrService attrService;

    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    // @RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params,
                  @PathVariable("catelogId") Long catelogId) {
        PageUtils page = attrGroupService.queryPage(params, catelogId);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    // @RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId) {
        AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        attrGroup.setCatelogPath(categoryService.findCatelogPath(attrGroup.getCatelogId()));
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    // @RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    // @RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    // @RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds) {
        attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

    /**
     * 根据分组ID
     * 获取属性分组的关联的所有属性
     *
     * @param attrGroupId
     * @return
     */
    @GetMapping("/{attrgroupId}/attr/relation")
    public R getAttrGroupAllAttr(@PathVariable("attrgroupId") long attrGroupId) {
        List<AttrEntity> attrs = attrGroupService.getAttrGroupAllAttr(attrGroupId);
        return R.ok().put("data", attrs);
    }

    /**
     * 根据属性分组查询可以关联的属性
     * 要求查出的属性没有被其他分组关联
     *
     * @param attrgroupId
     * @return
     */
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R getAttrGroupNoRelation(@RequestParam Map<String, Object> params,
                                    @PathVariable("attrgroupId") long attrgroupId) {
        PageUtils page = attrService.getNoAttrGroupRelation(params, attrgroupId);
        return R.ok().put("page", page);
    }

    /**
     * 删除属性分组与属性的关联关系
     *
     * @param relation 关联关系列表
     * @return
     */
    @PostMapping("/attr/relation/delete")
    public R deleteAttrAndAttrGroup(@RequestBody List<AttrGroupRelationVo> relation) {
        attrGroupService.delAttrRelationGroup(relation);
        return R.ok();
    }

    /**
     * 添加属性与分组关联关系
     */
    @PostMapping("/attr/relation")
    public R saveAttrAttrGroupRelation(@RequestBody List<AttrGroupRelationVo> vos) {
        attrAttrgroupRelationService.saveByAttrAndAttrGroup(vos);
        return R.ok();
    }

    /**
     * 获取分类下所有分组&关联属性
     * @param catelogId
     * @return
     */
    @GetMapping("/{catelogId}/withattr")
    public R getAttrByAttrGroupByCatelogId(@PathVariable("catelogId") long catelogId) {
        List<AttrAttrGroupVo> attrGroupVos = attrGroupService.getAttrByAttrGroupByCatelogId(catelogId);
        return R.ok().put("data", attrGroupVos);
    }
}
